const path = require('path');
const fs = require('fs').promises;
let moduleReg = /^\/@modules\//

function moduleResolvePlugin(context) {
  const { app, root } = context;

  app.use(async (ctx, next) => {

    if (!moduleReg.test(ctx.path)) {
      return next();
    }

    let id = ctx.path.replace(moduleReg, '');

    const modulesMap = {
      'vue': path.resolve(root, 'node_modules', '@vue/runtime-dom/dist/runtime-dom.esm-browser.js')
    }

    const result = await fs.readFile(modulesMap[id], 'utf-8');

    ctx.body = result;

    ctx.type = 'js';

  })
}

module.exports = moduleResolvePlugin;